package com.bodgittandscarper.brokerhmi.model;

import java.util.ArrayList;
import java.util.List;

/**
 * This is the <code>PropertyObserver</code> adding support for sending property change notifications.
 *  
 * It consists of a internal list holding observers(<code>Propertylistener</code>'s) 
 * and a set of methods for adding and removing listeners.
 * Whenever someone wants to send out a notification, it is done through the <code>notifyPropertyChange</code> method.
 * 
 * @author Morten Rummelhoff
 *
 */
public class PropertyObserver {

	private List<PropertyListener> propertyListenerList = new ArrayList<PropertyListener>();
	
	/**
	 * Sole constructor for PropertyObserver
	 */
	public PropertyObserver() {
		//nothing to to
	}
	
	/**
	 * Adds a PropertyListener to the list. No check is made if the listener already exist in the list. 
	 * 
	 * @param listener a listener to be added
	 */
	public void addPropertyListener(PropertyListener listener) {
		propertyListenerList.add(listener);
	}
	
	/**
	 * Removes a PropertyLstener from the list. If listener does not exit in list, nothing is done.
	 * 
	 * @param listener a listener to be deleted
	 */
	public void removePropertyListener(PropertyListener listener) {
		propertyListenerList.remove(listener);
	}
	
	/**
	 * Method for notifying about a property change. Calling this method will invoke <code>propertyChange(event, value)</code> 
	 * for all added <code>PropertyListener</code>'s in the list. 
	 *  
	 * @param event The event describing the type of property that has changed.
	 * @param value The new value of the property.
	 */
	public void notifyPropertyChange(ObservableEvent event, Object value) {
		for (PropertyListener listener : propertyListenerList) {
			listener.propertyChanged(event, value);
		}
	}
	
}
